<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\admin\model\Project;
use app\admin\model\ProjectPayment;
use app\admin\model\Report;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

use FFMpeg\FFMpeg;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

	public function index()
	{
		 try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        /* $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        $this->view->assign([
            'totaluser'         => User::count(),
            'totaladdon'        => $totaladdon,
            'totaladmin'        => Admin::count(),
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin'    => User::whereTime('logintime', 'today')->count(),
            'sevendau'          => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau'         => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu'          => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu'          => User::whereTime('jointime', '-7 days')->count(),
            'dbtablenums'       => count($dbTableList),
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums'    => Attachment::count(),
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist)); */
		
		$statisyear = intval($this->request->param('statisyear'));
		if(!$statisyear)
		{
			$statisyear = date('Y');
		}

		$start_statistime = strtotime($statisyear.'-01-01 00:00:00');
		$end_statistime = strtotime($statisyear.'-12-31 23:59:59');
		
		$todayNoPayProject = ProjectPayment::whereTime('paymenttime', 'today')->where('paymentstatus', 0)->group('project_id')->count();
		
		$starttime = Date::unixtime('day', 0);
        $endtime = Date::unixtime('day', 15, 'end');
		$fifteenDayNoPayProject = ProjectPayment::where('paymenttime', 'between time', [$starttime, $endtime])->where('paymentstatus', 0)->group('project_id')->count();
		
		$noBeginProject = Project::where('status', 0)->where('starttime', 'between time', [$start_statistime, $end_statistime])->count();
		$buyingProject = Project::where('status', 1)->where('starttime', 'between time', [$start_statistime, $end_statistime])->count();
		$startedProject = Project::where('status', 2)->where('starttime', 'between time', [$start_statistime, $end_statistime])->count();
		$finishedProject = Project::where('status', 3)->where('starttime', 'between time', [$start_statistime, $end_statistime])->count();
		
		$allProject = Project::where('starttime', 'between time', [$start_statistime, $end_statistime])->count();
		$allProjectMoney = Project::where('starttime', 'between time', [$start_statistime, $end_statistime])->sum('totalmoney');
		$finishedProjectMoney = Project::where('status', 3)->where('starttime', 'between time', [$start_statistime, $end_statistime])->sum('totalmoney');
		
		$unFinishedProject = $allProject-$finishedProject;
		$unFinishedProjectMoney = $allProjectMoney - $finishedProjectMoney;
		
		//项目年度执行率
		$yearPaymentMoney = ProjectPayment::where('paymenttime', 'between time', [$start_statistime, $end_statistime])->where('paymentstatus', 1)->sum('paymentmoney');
		$allProjectBidderMoney = Project::where('starttime', 'between time', [$start_statistime, $end_statistime])->sum('biddermoney');
		$yearExecutionRate = $allProjectBidderMoney?round($yearPaymentMoney*100/$allProjectBidderMoney, 2):0;
		
		$this->view->assign([
			'statisyear' => $statisyear,
			'todayNoPayProject' => $todayNoPayProject,
			'fifteenDayNoPayProject' => $fifteenDayNoPayProject,
			'noBeginProject' => $noBeginProject,
			'buyingProject' => $buyingProject,
			'startedProject' => $startedProject,
			'finishedProject' => $finishedProject,
			'allProject' => $allProject,
			'allProjectMoney' => $allProjectMoney,
			'finishedProjectMoney' => $finishedProjectMoney,
			'unFinishedProject' => $unFinishedProject,
			'unFinishedProjectMoney' => $unFinishedProjectMoney,
			'yearExecutionRate' => $yearExecutionRate,
		]);
		return $this->view->fetch();
	}
	
    /**
     * 查看
     */
    public function index1()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        $this->view->assign([
            'totaluser'         => User::count(),
            'totaladdon'        => $totaladdon,
            'totaladmin'        => Admin::count(),
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin'    => User::whereTime('logintime', 'today')->count(),
            'sevendau'          => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau'         => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu'          => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu'          => User::whereTime('jointime', '-7 days')->count(),
            'dbtablenums'       => count($dbTableList),
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums'    => Attachment::count(),
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));

        return $this->view->fetch();
    }
	
	public function test()
	{
		$ffmpeg = FFMpeg::create();
		$video = $ffmpeg->open('test.mp4');
		$video
			->filters()
			->resize(new \FFMpeg\Coordinate\Dimension(320, 240))
			->synchronize();
		$video
			->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(1))
			->save('frame.jpg');
		$video
			->save(new \FFMpeg\Format\Video\X264(), 'export-x264.mp4')
			->save(new \FFMpeg\Format\Video\WMV(), 'export-wmv.wmv')
			->save(new \FFMpeg\Format\Video\WebM(), 'export-webm.webm');
	}
}
